package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spi.service.contexts.Defaults;

import java.util.List;

@RestController
@Api("用户管理")
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;


    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation("用户信息分页查找")
    public ResponseResult findByPage(
            @ApiParam("查询的实体类")@RequestBody UserDto userDto,
            @ApiParam("查询页码")@RequestParam(defaultValue = "1")@PathVariable int pageNum,
            @ApiParam("查询大小")@RequestParam(defaultValue="10") @PathVariable int pageSize
            ){
        PageResponse resout = userService.findByPage(userDto,pageNum, pageSize);
        return ResponseResult.success(resout);
    }

    @PutMapping
    @ApiOperation("保存用户信息")
    public ResponseResult save(
            @ApiParam("用户信息")@RequestBody UserDto userDto
    ){
        userService.save(userDto);
        return ResponseResult.success();
    }

    @PatchMapping
    public ResponseResult update(
            @ApiParam("用户信息")@RequestBody UserDto userDto
    ){
        userService.update(userDto);
        return ResponseResult.success();
    }


    @ApiOperation("查询当前用户")
    @GetMapping("/current-user")
    public ResponseResult currentUser(){
        Long userId = UserThreadLocal.getMgtUserId();
        UserVo userVo =userService.findById(userId);
        return ResponseResult.success(userVo);
    }

    @ApiOperation("更新用户状态")
    @PutMapping("/is-enable/{id}/{status}")
    public ResponseResult updateStatus(
            @ApiParam("用户id")@PathVariable Long id,
            @ApiParam("状态")@PathVariable String status
    ){
        userService.updateByStatus(id,status);
        return ResponseResult.success();
    }

    @ApiOperation("查询用户列表")
    @PostMapping("/list")
    public ResponseResult list(
            @ApiParam("用户信息")
            @RequestBody UserDto userDto){

        List<UserVo> vo = userService.list(userDto);
        return ResponseResult.success(vo);
    }

    @ApiOperation("重置密码")
    @PostMapping("/reset-passwords/{userId}")
    public ResponseResult resetPassword(@PathVariable Long userId){
        userService.resetPassword(userId);
        return ResponseResult.success();
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/remove/{userIds}")
    public ResponseResult delete(@ApiParam("用户id")@PathVariable Long userIds){
        userService.delete(userIds);
        return ResponseResult.success();
    }
}
